package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TDoctorUser;
import com.ruoyi.project.system.service.ITDoctorUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 医生管理Controller
 *
 * @author 孙山伟
 * @date 2020-12-20
 */
@RestController
@RequestMapping("/system/doctoruser")
public class TDoctorUserController extends BaseController {
    @Autowired
    private ITDoctorUserService tDoctorUserService;

    /**
     * 查询医生管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDoctorUser tDoctorUser) {
        startPage();
        List<TDoctorUser> list = tDoctorUserService.selectTDoctorUserList(tDoctorUser);
        return getDataTable(list);
    }

    /**
     * 导出医生管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:export')")
    @Log(title = "医生管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TDoctorUser tDoctorUser) {
        List<TDoctorUser> list = tDoctorUserService.selectTDoctorUserList(tDoctorUser);
        ExcelUtil<TDoctorUser> util = new ExcelUtil<TDoctorUser>(TDoctorUser.class);
        return util.exportExcel(list, "doctoruser");
    }

    /**
     * 获取医生管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tDoctorUserService.selectTDoctorUserById(id));
    }

    /**
     * 新增医生管理
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:add')")
    @Log(title = "医生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDoctorUser tDoctorUser) {
        if (UserConstants.NOT_UNIQUE.equals(tDoctorUserService.checkUserNameUnique(tDoctorUser.getUserName()))) {
            return AjaxResult.error("新增用户'" + tDoctorUser.getUserName() + "'失败，登录账号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(tDoctorUserService.checkPhoneUnique(tDoctorUser))) {
            return AjaxResult.error("新增用户'" + tDoctorUser.getUserName() + "'失败，手机号码已存在");
        }
        return toAjax(tDoctorUserService.insertTDoctorUser(tDoctorUser));
    }

    /**
     * 修改医生管理
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:edit')")
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDoctorUser tDoctorUser) {
        if (UserConstants.NOT_UNIQUE.equals(tDoctorUserService.checkPhoneUnique(tDoctorUser))) {
            return AjaxResult.error("修改用户'" + tDoctorUser.getUserName() + "'失败，手机号码已存在");
        }
        return toAjax(tDoctorUserService.updateTDoctorUser(tDoctorUser));
    }

    /**
     * 删除医生管理
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:remove')")
    @Log(title = "医生管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDoctorUserService.deleteTDoctorUserByIds(ids));
    }


    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:doctoruser:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody TDoctorUser tDoctorUser) {

        tDoctorUser.setPassword(SecurityUtils.encryptPassword(tDoctorUser.getPassword()));
        tDoctorUser.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tDoctorUserService.resetPwd(tDoctorUser));
    }

}
